package com.example.shopping.service.impl;

import com.example.shopping.mapper.CartsMapper;
import com.example.shopping.service.Carts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/6/29 19:27
 * @version: v1.0
 */
@Service
public class CartsImpl implements Carts {

    @Autowired
    CartsMapper cartsMapper;

    @Override
    public Boolean updateCarts(com.example.shopping.pojo.Carts carts) {
        int update = cartsMapper.update(carts);
        return update == 1 ? true : false;
    }

    @Override
    public Boolean findById(Integer id1, Integer id2) {
        com.example.shopping.pojo.Carts carts = cartsMapper.findById(id1, id2);
        return carts == null ? true : false;
    }

    @Override
    public Boolean addCarts(com.example.shopping.pojo.Carts carts){
        com.example.shopping.pojo.Carts byId = cartsMapper.findById(carts.getCustomerId(), carts.getProductId());
        if (byId == null){
            int insert = cartsMapper.insert(carts);
            return insert == 1 ? true : false;
        }
        Integer quantity1 = carts.getQuantity();

        Integer quantity2 = byId.getQuantity();
        Integer quantity = quantity1 + quantity2;
        byId.setQuantity(quantity);
        Boolean aBoolean = updateCarts(byId);
        return aBoolean ? true : false;
    }

    @Override
    public List<com.example.shopping.pojo.Carts> findAll(){
        List<com.example.shopping.pojo.Carts> carts = cartsMapper.selectAll();
        return carts;
    }
}
